class Group < ActiveRecord::Base

  attr_protected []  

  attr_protected []

  has_and_belongs_to_many :users, :uniq => true
  belongs_to :enterprise
  
  validates :enterprise_id, :presence => true
  validates :name, :presence => true
  validates :name, :uniqueness => { :scope => :enterprise_id }
  
  default_scope lambda { where( :enterprise_id => Enterprise.current_id ).order('position asc') }

  before_save do |group|
    group.position ||= Group.count + 1
  end

  def admin?
    name == '管理员' or name == '管理员组'
  end

  def position!(new_position)
    self.position = new_position
    self.save(:validate => false)
  end
  
end
